package com.biz.primus.base.exception.utils;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.base.exception.ExceptionType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 *
 * 断言工具,判断是否满足断言,如果不满足,则抛出BizSilentException
 * 需要传入ExceptionType,如果同时传入String message,则该message会覆盖ExceptionType的description
 *
 * @author yanweijin
 * @date 2017/9/20
 */
public class AssertUtils {


	public static void isTrue(boolean expression, ExceptionType type) throws BizSilentException {
		if (!expression) {
			throw new BizSilentException(type);
		}
	}

	public static void isNull(Object object, ExceptionType type) throws BizSilentException {
		if (object != null) {
			throw new BizSilentException(type);
		}
	}

	public static void notNull(Object object, ExceptionType type) throws BizSilentException {
		if (object == null) {
			throw new BizSilentException(type);
		}
	}

	public static void hasLength(String text, ExceptionType type) throws BizSilentException {
		if (!StringUtils.hasLength(text)) {
			throw new BizSilentException(type);
		}
	}
	public static void hasText(String text, ExceptionType type) throws BizSilentException {
		if (!StringUtils.hasText(text)) {
			throw new BizSilentException(type);
		}
	}

    public static void hasText(String text, ExceptionType type, Object... args) throws BizSilentException {
        if (!StringUtils.hasText(text)) {
            throw new BizSilentException(type, args);
        }
    }

	public static void doesNotContain(String textToSearch, String substring, ExceptionType type) throws BizSilentException {
		if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
				textToSearch.contains(substring)) {
			throw new BizSilentException(type);
		}
	}
	public static void notEmpty(Object[] array, ExceptionType type) throws BizSilentException {
		if (ObjectUtils.isEmpty(array)) {
			throw new BizSilentException(type);
		}
	}
	public static void noNullElements(Object[] array, ExceptionType type) throws BizSilentException {
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					throw new BizSilentException(type);
				}
			}
		}
	}

	public static void notEmpty(Collection<?> collection, ExceptionType type) throws BizSilentException {
		if (CollectionUtils.isEmpty(collection)) {
			throw new BizSilentException(type);
		}
	}

	public static void notEmpty(Map<?, ?> map, ExceptionType type) throws BizSilentException {
		if (CollectionUtils.isEmpty(map)) {
			throw new BizSilentException(type);
		}
	}


	public static void isTrue(boolean expression, ExceptionType type, String message)  throws BizSilentException {
		if (!expression) {
			throw new BizSilentException(type, message);
		}
	}

    public static void isTrue(boolean expression, ExceptionType type, Object... args)  throws BizSilentException {
        if (!expression) {
            throw new BizSilentException(type, args);
        }
    }

	public static void isNull(Object object, ExceptionType type, String message)  throws BizSilentException {
		if (object != null) {
			throw new BizSilentException(type, message);
		}
	}

	public static void notNull(Object object, ExceptionType type, String message)  throws BizSilentException {
		if (object == null) {
			throw new BizSilentException(type, message);
		}
	}

    public static void notNull(Object object, ExceptionType type, Object... args)  throws BizSilentException {
        if (object == null) {
            throw new BizSilentException(type, args);
        }
    }

	public static void hasLength(String text, ExceptionType type, String message)  throws BizSilentException {
		if (!StringUtils.hasLength(text)) {
			throw new BizSilentException(type, message);
		}
	}
	public static void hasText(String text, ExceptionType type, String message)  throws BizSilentException {
		if (!StringUtils.hasText(text)) {
			throw new BizSilentException(type, message);
		}
	}
	public static void doesNotContain(String textToSearch, String substring, ExceptionType type, String message)  throws BizSilentException {
		if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
				textToSearch.contains(substring)) {
			throw new BizSilentException(type, message);
		}
	}
	public static void notEmpty(Object[] array, ExceptionType type, String message)  throws BizSilentException {
		if (ObjectUtils.isEmpty(array)) {
			throw new BizSilentException(type, message);
		}
	}
	public static void noNullElements(Object[] array, ExceptionType type, String message)  throws BizSilentException {
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					throw new BizSilentException(type, message);
				}
			}
		}
	}

	public static void notEmpty(Collection<?> collection, ExceptionType type, String message)  throws BizSilentException {
		if (CollectionUtils.isEmpty(collection)) {
			throw new BizSilentException(type, message);
		}
	}

	public static void notEmpty(Map<?, ?> map, ExceptionType type, String message)  throws BizSilentException {
		if (CollectionUtils.isEmpty(map)) {
			throw new BizSilentException(type, message);
		}
	}


}
